var server = "http://ydev.netii.net/api.php";
var log;
onload = init;

function init() {

	log = document.getElementById("log");
	var r = authenticateUser();
	if (!r) {
		window.location = ('http://ydev.netii.net/login.html');
		return;
	}
	getAndSetProjects();
	getAndSetMessages();
	log.innerHTML = "Woreks";
	var btnSubmit=document.getElementById("btnSubmit");//$("#btnSubmit");
	btnSubmit.onclick = function() {
		submitMessage();
	}


}

function submitMessage(){
	var string = $("#title").val();
	if (string.length <= 0) {
		// If null display and alert box
		alert("Please enter title");
		// Place the cursor on the field for revision
		$("#title").focus();
		// return false to stop further processing
		return (false);
	}
	string = $("#details").val();
	if (string.length <= 0) {
		// If null display and alert box
		alert("Please enter details");
		// Place the cursor on the field for revision
		$("#details").focus();
		// return false to stop further processing
		return (false);
	} else {

		var params = new Array();
		params['action'] = "addMessage";
		params['title'] = $("#title").val();
		params['details'] = $("#details").val();
		var param = createURLParams(params);
		var URL = server + "?" + param;
		var request = myCall(URL);

		request.done(function(result) {
			var obj = jQuery.parseJSON(result);
			if (obj.status == 1) {
			//	alert(obj.query);
			getAndSetMessages();
			}
			//alert(result);
			//return request;
		});
		request.fail(function(jqXHR, textStatus) {
			alert("Request failed: " + textStatus);
		});

		//alert(result);
	}
}

function getAndSetProjects() {
	var params = new Array();
	params['action'] = "getProjects";
	var param = createURLParams(params);
	var URL = server + "?" + param;
	var request = myCall(URL);

	request.done(function(result) {
		var obj = jQuery.parseJSON(result);
		if (obj.status == 1) {
			setProjectsNav(obj.projects);
		}
		//alert(result);
		//return request;
	});
	request.fail(function(jqXHR, textStatus) {
		alert("Request failed: " + textStatus);
	});

	//alert(result);
}

function setProjectsNav(data) {
	$.each(data, function(key, value) {
		$("#projectsTab").append("<li><a onclick='projectClicked(this)' href='#'>" + value + "</a></li>");
	});

}

function getAndSetMessages() {
	var params = new Array();
	params['action'] = "getMessages";
	var param = createURLParams(params);
	var URL = server + "?" + param;
	var request = myCall(URL);

	request.done(function(result) {
		var obj = jQuery.parseJSON(result);
		if (obj.status == 1) {
			setMessages(obj.messages);
		}
		//alert(result);
		//return request;
	});
	request.fail(function(jqXHR, textStatus) {
		alert("Request failed: " + textStatus);
	});

	//alert(result);
}

function setMessages(data) {
	//var request = myCall('http://ydev.netii.net/messageForm.txt');
	//request.done(function(result) {
		//var obj = jQuery.parseJSON(result);
		$.each(data, function(key, value) {
		$("#messages").append(formatString("<div><div style='display: inline-block; width:100%'><h1 id = 'title' style='float: left'>%@</h1> <p id='date'style='float: right'>%@</p></div><p id='details' style='border: thin solid #FFFFFF; word-wrap:break-word; display: block; text-align: left;'>%@&nbsp;</p></div>", value));
	});
	//});
	//request.fail(function(jqXHR, textStatus) {
	//	alert("Request failed: " + textStatus);
//	});
	

}

function formatString(string, strings){
	$.each(strings, function(key, value) {
		string=string.replace("%@", value)
	});
	return string;
}

function projectClicked(ob) {
	alert(ob.textContent);
}
